package com.example.main;

import android.content.res.AssetManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;


import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.sl.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;

public class EasyExcelActivity extends FragmentActivity {

    private StringBuffer stringBuffer = new StringBuffer();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_easy_excel);
        AssetManager assetManager = getAssets();
        try {
            InputStream inputStream = assetManager.open("testExcel.xlsx");
            //readPoiExcel(inputStream);
            hutoolRead(inputStream);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void hutoolRead(InputStream inputStream) {
        try {
//            String excelPath = "file://testExcel.xlsx";
//            File excel = FileUtil.file(excelPath);
//            if (excel.isFile() && excel.exists()) {   //判断文件是否存在
            ExcelReader reader = ExcelUtil.getReader(inputStream);
            //开始解析
            List<Map<String, Object>> list = reader.readAll();
            for (int i = 0; i < list.size(); i++) {
                Map<String, Object> value = list.get(i);
                System.out.println("dddd:" + value.toString());
            }

//            } else {
//                System.out.println("找不到指定的文件");
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    protected String getCellAsString(Row row, int c, FormulaEvaluator formulaEvaluator) {
        String value = "";
        try {
            Cell cell = row.getCell(c);
            CellValue cellValue = formulaEvaluator.evaluate(cell);
            switch (cellValue.getCellType()) {
                case Cell.CELL_TYPE_BOOLEAN:
                    value = "" + cellValue.getBooleanValue();
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    double numericValue = cellValue.getNumberValue();
                    if (HSSFDateUtil.isCellDateFormatted(cell)) {
                        double date = cellValue.getNumberValue();
                        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yy");
                        value = formatter.format(HSSFDateUtil.getJavaDate(date));
                    } else {
                        value = "" + numericValue;
                    }
                    break;
                case Cell.CELL_TYPE_STRING:
                    value = "" + cellValue.getStringValue();
                    break;
                default:
            }
        } catch (NullPointerException e) {
            stringBuffer.append("null\n");
            printlnToUser();
        }
        return value;
    }

    /**
     * print line to the output TextView
     */
    private void printlnToUser() {
        System.out.println(stringBuffer.toString());
    }

}
